﻿using System;
using SimpleServiceBus.Utilities.ExpressionEval;

namespace SimpleServiceBus.Bus.MessageManagement
{
    public class ExpressionMessagePredicate : IMessagePredicate
    {
        private readonly IMessageExpressionEvaluator _eval;
        private readonly string _expression;

        public ExpressionMessagePredicate(string expression,IMessageExpressionEvaluator eval)
        {
            _eval = eval;
            _expression = expression;
        }
        public bool Matches(MessageEnvelope messageEnvelope)
        {
            if (_eval == null)
                return false;
            if (String.IsNullOrEmpty(_expression))
                return true;

            return _eval.Evaluate<bool>(_expression,messageEnvelope);
        }
    }
}
